<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <title>我的收藏 - 企业培训平台</title>

    <style>
        /* --- 全局布局与变量 (与其他页面保持一致) --- */
        :root {
            --sidebar-width: 240px;
            --primary-color: #007bff;
            --danger-color: #dc3545;
            --border-color: #dee2e6;
            --card-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            background-color: #f4f5f7;
            margin: 0;
            color: #333;
        }

        /* --- 核心布局修复 --- */
        .main-content {
            margin-left: var(--sidebar-width); /* 关键: 为左侧固定的侧边栏留出空间 */
            padding: 30px;
        }

        /* --- 页面容器与标题 --- */
        .container-card {
            max-width: 1200px; /* 可以适当调整最大宽度 */
            margin: 0 auto;
            background-color: #ffffff;
            border-radius: 8px;
            box-shadow: var(--card-shadow);
            padding: 25px 30px;
        }

        .page-header {
            margin: 0 0 25px 0;
            padding-bottom: 20px;
            border-bottom: 1px solid var(--border-color);
            font-size: 1.8em;
            color: #333;
        }

        /* --- 课程卡片列表样式 --- */
        .course-list {
            display: grid;
            /* 响应式网格布局，每张卡片最小300px，最大1fr */
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 25px; /* 卡片之间的间距 */
            list-style: none;
            padding: 0;
        }

        .course-item {
            background-color: #fff;
            border-radius: 8px;
            overflow: hidden; /* 确保图片圆角生效 */
            box-shadow: var(--card-shadow);
            transition: transform 0.2s ease-in-out, box-shadow 0.3s ease;
            display: flex;
            flex-direction: column; /* 让卡片内容垂直排列 */
            opacity: 1; /* JS动画需要 */
        }
        .course-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
        }

        .course-item img {
            width: 100%;
            height: 180px;
            object-fit: cover; /* 图片不变形，填满容器 */
        }

        .course-details {
            padding: 20px;
            flex-grow: 1; /* 让此区域填满剩余空间，将按钮推到底部 */
            display: flex;
            flex-direction: column;
        }

        .course-details h2 {
            font-size: 1.25em;
            color: #212529;
            margin: 0 0 10px 0;
        }

        .course-details p {
            font-size: 0.9em;
            color: #6c757d;
            line-height: 1.6;
            margin: 0 0 15px 0;
            flex-grow: 1; /* 占据空间，确保简介长短不一的卡片，按钮也能对齐 */
        }

        .button-group {
            display: flex; /* 让按钮并排 */
            gap: 10px; /* 按钮之间的间距 */
        }

        .btn {
            flex: 1; /* 让两个按钮平分空间 */
            display: inline-block;
            padding: 10px 15px;
            color: white;
            text-align: center;
            border-radius: 5px;
            text-decoration: none;
            font-size: 0.9em;
            font-weight: 500;
            transition: background-color 0.2s ease, filter 0.2s;
            cursor: pointer;
            border: none;
        }
        .btn:hover {
            filter: brightness(90%);
        }

        .btn-primary { background-color: var(--primary-color); }
        .btn-danger { background-color: var(--danger-color); }

        .no-courses {
            text-align: center;
            font-size: 1.1em;
            color: #777;
            padding: 60px 20px;
            background-color: #fafafa;
            border: 1px dashed #ccc;
            border-radius: 8px;
        }
        .no-courses a {
            color: var(--primary-color);
            font-weight: 500;
        }
    </style>
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>

<div class="main-content">
    <div class="container-card">
        <h1 class="page-header">我的收藏</h1>

        <div th:if="${#lists.isEmpty(courses)}" class="no-courses">
            <p>您还没有收藏任何课程。</p>
            <p><a th:href="@{/course/browse}">前往课程列表浏览课程 &rarr;</a></p>
        </div>

        <ul class="course-list" th:if="${not #lists.isEmpty(courses)}">
            <li th:each="course : ${courses}" class="course-item" th:id="'course-item-' + ${course.id}">
                <img th:src="${course.couPic != null and course.couPic != '' ? course.couPic : 'https://placehold.co/400x225/e9ecef/6c757d?text=No+Image'}"
                     onerror="this.onerror=null;this.src='https://placehold.co/400x225/e9ecef/6c757d?text=No+Image';"
                     alt="Course Cover">
                <div class="course-details">
                    <h2 th:text="${course.couName}">课程名称</h2>
                    <p th:text="${#strings.abbreviate(course.couIntroduction, 100)}">课程简介...</p>
                    <div class="button-group">
                        <a th:href="@{/course/detail/{id}(id=${course.id})}" class="btn btn-primary">查看详情</a>
                        <button class="btn btn-danger cancel-collect-button" th:data-course-id="${course.id}">取消收藏</button>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>

<script th:inline="javascript">
    document.addEventListener('DOMContentLoaded', function() {
        const cancelButtons = document.querySelectorAll('.cancel-collect-button');

        cancelButtons.forEach(button => {
            button.addEventListener('click', function() {
                const courseId = this.getAttribute('data-course-id');
                const courseItem = document.getElementById('course-item-' + courseId);

                if (confirm('您确定要取消收藏该课程吗？')) {
                    fetch(`/course/api/collect/${courseId}`, {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    })
                        .then(response => {
                            if (!response.ok) {
                                return response.json().then(err => { throw new Error(err.message || '网络响应错误'); });
                            }
                            return response.json();
                        })
                        .then(data => {
                            if (data.success && !data.isCollected) {
                                if (courseItem) {
                                    // 添加一个平滑的消失动画
                                    courseItem.style.transition = 'transform 0.4s ease, opacity 0.4s ease';
                                    courseItem.style.transform = 'scale(0.95)';
                                    courseItem.style.opacity = '0';
                                    setTimeout(() => {
                                        courseItem.remove();
                                        const courseList = document.querySelector('.course-list');
                                        // 检查列表是否已空
                                        if (courseList && courseList.children.length === 0) {
                                            const container = document.querySelector('.container-card');
                                            courseList.remove();
                                            const noCoursesMessage = `
                                        <div class="no-courses">
                                            <p>您还没有收藏任何课程。</p>
                                            <p><a href="/course/browse">前往课程列表浏览课程 &rarr;</a></p>
                                        </div>`;
                                            container.insertAdjacentHTML('beforeend', noCoursesMessage);
                                        }
                                    }, 400); // 动画时长
                                }
                            } else {
                                alert('操作失败：' + (data.message || '未知错误，请刷新页面后重试。'));
                            }
                        })
                        .catch(error => {
                            console.error('取消收藏操作失败:', error);
                            alert('操作失败: ' + error.message);
                        });
                }
            });
        });
    });
</script>

</body>
</html>